home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- //
- // XMSTEST.CPP: Test program for XMS class.
- // Copyright (c) J.English 1993.
- // Author's address: je@unix.brighton.ac.uk
- //
- // Permission is granted to use copy and distribute the
- // information contained in this file provided that this
- // copyright notice is retained intact and that any software
- // or other document incorporating this file or parts thereof
- // makes the source code for the library of which this file
- // is a part freely available.
- //
- // MODIFIED BY MAXTAL P/L. Added more exhaustive XMS IO test:
- // option 6 in the menu.
- //--------------------------------------------------------------------------
- //
- // Revision history:
- // 2.0 Nov 1993 Initial coding
- // 2.1 Aug 1994 Added random XMS IO test
- //--------------------------------------------------------------------------
-
- #include "xms.h"
- #include "doserror.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
-
- //--------------------------------------------------------------------------
- //
- // Global data.
- //
- DOSerror e; // guard against critical errors and control-breaks
- XMS* desc [100]; // array of pointers to XMS descriptors
- int ndesc = 0; // number of descriptors
-
-
- //--------------------------------------------------------------------------
- //
- // Function prototypes.
- //
- char menu (); // display menu & get user's choice
- void XMSstats (); // 1. XMS statistics
- void blocksizes (); // 2. List block sizes
- void allocate (); // 3. Allocate block
- void deallocate (); // 4. Deallocate block
- void copy (); // 5. Copy block
- void randcopy (); // 6. Copy block
- void resize (); // 7. Resize block
-
- //--------------------------------------------------------------------------
- //
- // Main program.
- //
- // A menu of choices is displayed, allowing the user to exercise
- // each of the XMS functions available (with the exception of
- // XMS-to-XMS copying).
- //
- void main ()
- {
- XMSstats();
-
- char choice;
- for (;;)
- {
- choice = menu ();
- if (choice == 'X')
- break;
-
- switch (choice)
- {
- case '1':
- XMSstats ();
- break;
- case '2':
- blocksizes ();
- break;
- case '3':
- allocate ();
- break;
- case '4':
- deallocate ();
- break;
- case '5':
- copy ();
- break;
- case '6':
- randcopy ();
- break;
- case '7':
- resize ();
- break;
- }
- }
- for (int i = 0; i < ndesc; i++)
- delete desc[i];
- }
-
- //--------------------------------------------------------------------------
- //
- // Display menu and get user choice.
- //
- // The choice must be a line containing a single character from 1 to 6
- // (or X for exit).
- //
- char menu ()
- {
- printf ("\nChoose desired operation:\n"
- " 1) XMS statistics\n"
- " 2) List block sizes\n"
- " 3) Allocate block\n"
- " 4) Deallocate block\n"
- " 5) Copy block\n"
- " 6) Random Write test\n"
- " 7) Resize block\n"
- " X) Exit program\n");
- char buff [80];
- for (;;)
- {
- printf ("Enter your choice: ");
- fgets (buff, 80, stdin);
- if ((buff[0] == 'x' || buff[0] == 'X') && buff[1] == '\n')
- return 'X';
- if (buff[0] < '1' || buff[0] > '6' || buff[1] != '\n')
- printf ("Invalid choice!\a\n");
- else
- break;
- }
- printf ("\n");
- return buff[0];
- }
-
-
- //--------------------------------------------------------------------------
- //
- // Display XMS statistics.
- //
- void XMSstats ()
- {
- printf ("XMS available: %ld bytes\nLargest block: %ld bytes\n",
- XMS::available(), XMS::largest());
- }
-
-
- //--------------------------------------------------------------------------
- //
- // List block sizes.
- //
- void blocksizes ()
- {
- int flag = 0;
- for (int i = 0; i < ndesc; i++)
- { if (desc[i] != 0)
- { printf ("Block %d: size = %ld\n", i+1, desc[i]->size());
- flag = 1;
- }
- }
- if (flag == 0)
- printf ("No blocks allocated!\n");
- }
-
- //--------------------------------------------------------------------------
- //
- // Allocate block.
- //
- // Attempt to allocate a new block of a specified size and report the
- // result.
- //
- void allocate ()
- {
- printf ("Enter block size: ");
- long size;
- scanf ("%ld", &size);
- while (getchar() != '\n')
- continue;
- desc [ndesc++] = new XMS (size);
- printf ("Block %d: ", ndesc);
- printf ("requested %ld bytes, granted %ld bytes.\n",
- size, desc[ndesc-1]->size());
- if (!desc[ndesc-1]->valid())
- delete desc[--ndesc];
- }
-
-
- //--------------------------------------------------------------------------
- //
- // Deallocate block.
- //
- // Attempt to deallocate an existing block and report the result.
- //
- void deallocate ()
- {
- printf ("Enter block number: ");
- int b;
- scanf ("%d", &b); b--;
- while (getchar() != '\n')
- continue;
- if (b < 0 || b >= ndesc || desc[b] == 0)
- printf ("No such block!\n");
- else
- { delete desc[b];
- desc[b] = 0;
- printf ("Block %d deallocated.\n", b+1);
- }
- }
-
- //--------------------------------------------------------------------------
- //
- // Copy to/from XMS.
- //
- // Prompts for a block number and a transfer size. An array of random
- // numbers of the specified size is created in conventional memory,
- // copied to the specified block starting at the specified offset,
- // copied back to conventional memory, and finally compared with the
- // original block.
- //
- void copy ()
- {
- printf ("Enter block number: ");
- int b;
- scanf ("%d", &b); b--;
- while (getchar() != '\n')
- continue;
- if (b < 0 || b >= ndesc || desc[b] == 0)
- { printf ("No such block!\n");
- return;
- }
-
- printf ("Enter transfer size: ");
- unsigned s;
- scanf ("%u", &s);
- while (getchar() != '\n')
- continue;
- char* x = new char [s];
- char* y = new char [s];
- if (x == 0 || y == 0)
- { printf ("Not enough real memory!\n");
- return;
- }
-
- printf ("Enter block offset: ");
- long p;
- scanf ("%ld", &p);
- while (getchar() != '\n')
- continue;
- printf ("Copying %u bytes to/from XMS block %d, offset %ld.\n", s, b, p);
- printf ("Generating random data... "); fflush (stdout);
- randomize ();
- for (unsigned i = 0; i < s; i++)
- x[i] = random (256);
- printf ("done.\n");
-
- XMS::error r = XMS::copy (desc[b]->at(p), x, s);
- printf ("Random data copied to XMS, result code %02X\n", int(r));
- if (r == XMS::SUCCESS)
- {
- // MODIFIED BY MAXTAL TO SCRAMBLE BEFORE CHECKING!
- for (unsigned i = 0; i < s; i++)
- y[i] = random (256);
-
- r = XMS::copy (y, desc[b]->at(p), s);
- printf ("Data copied back from XMS, result code %02X\n", int(r));
- if (r == XMS::SUCCESS)
- { for (i = 0; i < s; i++)
- { if (x[i] != y[i])
- { printf ("Verification error at offset %u.\n", i);
- break;
- }
- }
- if (i == s)
- printf ("Copy verified successfully.\n");
- }
- }
- delete x;
- delete y;
- }
-
- //--------------------------------------------------------------------------
- //
- // RANDOM Copy to/from XMS.
- //
- void randcopy ()
- {
- printf ("Enter block size: ");
- int blksize;
- scanf ("%d", &blksize);
- while (getchar() != '\n')
- continue;
-
- printf ("Enter number of iterations size: ");
- int iterations;
- scanf ("%d", &iterations);
- while (getchar() != '\n')
- continue;
-
- XMS *xmsBlock = new XMS(blksize); // blksize byte block
- XMS *xmsBlock2 = new XMS(blksize); // blksize byte block
- unsigned char* x = (char*)malloc(blksize);
- unsigned char* y = (char*)malloc(blksize);
- if (x == 0 || y == 0)
- { printf ("Not enough real memory!\n");
- return;
- }
-
- for (int i = 0; i < blksize; i++)
- {
- y[i] = x[i] = random (256);
- }
- XMS::copy (xmsBlock->at(0), x, blksize);
- XMS::copy (xmsBlock2->at(0), x, blksize);
-
- for(int j = 0; j<iterations; ++j)
- {
- unsigned long start = random(blksize);
- unsigned long len = random(blksize-start);
- printf ("%lu bytes XMS offset %ld ... ", len, start);
-
- for (unsigned i = start; i < start+len; i++) x[i] = random (256);
- XMS::copy (xmsBlock->at(start), x+start, len);
- XMS::copy (xmsBlock2->at(start), xmsBlock->at(start), len);
-
- memset(y,0,blksize);
- XMS::copy (y+start, xmsBlock->at(start), len);
- for (i = 0; i < start; i++)
- { if (0 != y[i])
- { printf ("Verification error 1 at byte %u.\n", i);
- return;
- }
- }
- for (i = start; i < start+len; i++)
- { if (x[i] != y[i])
- { printf ("Verification error 2 at byte %u.\n", i);
- return;
- }
- }
- for (i = start+len; i < blksize; i++)
- { if (0 != y[i])
- { printf ("Verification error 3 at byte %u.\n", i);
- return;
- }
- }
-
- memset(y,0,blksize);
- XMS::copy (y, xmsBlock->at(0), blksize);
- for (i = 0; i < blksize; i++)
- { if (x[i] != y[i])
- { printf ("Verification error 4 at byte %u.\n", i);
- return;
- }
- }
-
- memset(y,0,blksize);
- XMS::copy (y, xmsBlock2->at(0), blksize);
- for (i = 0; i < blksize; i++)
- { if (x[i] != y[i])
- { printf ("Verification error 5 at byte %u.\n", i);
- return;
- }
- }
- printf("\n");
- }
- free(x);
- free(y);
- delete xmsBlock;
- delete xmsBlock2;
- }
-
- //--------------------------------------------------------------------------
- //
- // Resize an existing block.
- //
- // Attempt to resize an existing XMS allocation and report the result.
- //
- void resize ()
- {
- printf ("Enter block number: ");
- int b;
- scanf ("%d", &b); b--;
- while (getchar() != '\n')
- continue;
- if (b < 0 || b >= ndesc || desc[b] == 0)
- { printf ("No such block!\n");
- return;
- }
-
- printf ("Current size: %ld\nEnter new size: ", desc[b]->size());
- long size;
- scanf ("%ld", &size);
- while (getchar() != '\n')
- continue;
- XMS::error r = desc[b]->resize (size);
- if (r == XMS::SUCCESS)
- printf ("Requested %ld, granted %ld.\n", size, desc[b]->size());
- else
- printf ("Resize failed: error code = %02X\n", int(r));
- }
-